package com.hrbxlh.actionImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.hrbxlh.dao.ProfessionDao;
import com.hrbxlh.util.pageUtil;

@Component("professionActionImpl")
@Transactional
public class ProfessionActionImpl {
	
	@Resource(name = "professionDao")
	private ProfessionDao professionDao;
	
	@Resource(name = "departMentActionImpl")
	private DepartMentActionImpl departMentActionImpl;

	org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(ProfessionActionImpl.class.getName());
	
	/**
	 * @description 获取全部专业信息
	 * @param Map<String, Object> 如果有查询条件，就将条件set到map中，
	 *        如果是数据库存在的字段，key值要与column的值相同。
	 * @return List<Map<String,Object>> (院系集合)
	 * @throws Exception
	 */
	public List<Map<String, Object>> getProfessionList(Map<String, Object> paramMap) throws Exception {

		List<Map<String, Object>> returnList = new ArrayList<Map<String, Object>>();
		try {
			if(paramMap.get("pageNow")!=null&&paramMap.get("pageSize")!=null){
				int pageNow = Integer.parseInt(paramMap.get("pageNow").toString());
				int pageSize = Integer.parseInt(paramMap.get("pageSize").toString());
				int startNum = pageUtil.getSqlStartPos(pageNow, pageSize);
				paramMap.put("startNum", startNum);
			}
			returnList=professionDao.getProfessionList(paramMap);
		} catch (Exception e) {
			logger.error("获取全部专业信息异常getProfessionList", e);
			throw e;
		}
		return returnList;
	}
	
	/**
	 * @description 删除专业
	 * @param Map<String,Object> paramMap
	 * @return Map result存成功失败标识   failMsg存错误信息
	 * @throws Exception
	 */
	public Map<String, Object> delProfessions(List<String> paramList) throws Exception{
		
		Map<String, Object> result = new HashMap<String, Object>();
		//存储可以删除的数量
		int trueLength = 0; 
		try{
			if(paramList != null && paramList.size() != 0){
				String[] tables = {"class","course","relation","sys_user"};
				String[] errMsg = {"班级","课程","课表","人员"};
				Map<String, Object> resultMap = new HashMap<String, Object>();
				for(int i = 0; i< paramList.size(); i++){
					resultMap = departMentActionImpl.getDepartmentDelMark("专业",tables, errMsg, "professionId", Integer.parseInt(paramList.get(i)));	
					boolean resultMark = (Boolean)resultMap.get("result");
					trueLength = (resultMark == true) ? (trueLength+1) : trueLength;
				}
				if(trueLength == paramList.size()){
					//删除专业信息
					professionDao.delProfessions(paramList);
					//删除该专业的专业方向信息
					professionDao.delDirectByProfessionId(paramList);
					//删除该专业与地点的关联信息
					professionDao.delSiteReProfessionById(paramList);
					result.put("result", true);
				}else{
					result.put("result", false);
					result.put("failMsg", resultMap.get("failMsg"));
				}	
			}
			logger.info("删除专业，参数为："+paramList.toString());
		}catch(Exception e){
			logger.error("删除专业异常delProfessions", e);
			throw e;
		}
		return result;
	}

	/**
	 * @description 添加专业
	 * @param Map<String,Object> paramMap  专业名称
	 * @return Map<String, String> result true成功，false失败 errorMsg错误信息
	 * @throws Exception
	 */
	public Map<String, Object> addProfession(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		boolean result = false;
		String failMsg = "";
		try {
//			//1013过滤输入参数
//			String professionName = ParamConvertUtil.replaceHTMLCodeOne((String)paramMap.get("professionName"));
//			paramMap.put("professionName", professionName);
			List<Map<String, Object>> professionList = professionDao.getProfessionByName(paramMap);
			//判断专业名重复
			if(professionList != null && professionList.size() != 0){
				failMsg = "该专业名已存在";
			}else{
				//添加专业
				professionDao.addProfession(paramMap);
				result = true;
				resultMap.put("professionId", paramMap.get("professionId"));
			}
			if(!result){
				resultMap.put("failMsg", failMsg);
			}
			resultMap.put("result", result);
			logger.info("添加专业，参数为："+paramMap.toString());
		} catch (Exception e) {
			logger.error("添加专业异常addProfession", e);
			throw e;
		}
		return resultMap;
	}
	
	/**
	 * @description 查询专业信息
	 * @param Map<String,Object> paramMap  专业id
	 * @return Map<String, String> 
	 * @throws Exception
	 */
	public Map<String, Object> getProfessionDetail(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = professionDao.getProfessionDetail(paramMap);
		} catch (Exception e) {
			logger.error("查询专业信息异常getProfessionDetail", e);
			throw e;
		}
		return resultMap;
	}
	
	/**
	 * @description 修改专业
	 * @param Map<String,Object> paramMap  专业id
	 * @return Map<String, String> result true成功，false失败 failMsg错误信息
	 * @throws Exception
	 */
	public Map<String, Object> saveProfession(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
//			//1013过滤输入
//			String professionName = ParamConvertUtil.replaceHTMLCodeOne((String)paramMap.get("professionName"));
//			paramMap.put("professionName", professionName);
			//按名字查询查询专业
			List<Map<String, Object>> professionList = professionDao.getProfessionByName(paramMap);
			//判断专业名重复
			if(professionList != null && professionList.size() != 0){
				resultMap.put("result", false);
				resultMap.put("failMsg", "该专业名已存在");
			}else{
				//修改专业
				professionDao.saveProfession(paramMap);
				resultMap.put("result", true);
			}
			logger.info("修改专业，参数为："+paramMap.toString());
		} catch (Exception e) {
			logger.error("修改专业异常saveProfession", e);
			throw e;
		}
		return resultMap;
	}
	
	/**
	 * @description 查询某个专业下的教师信息
	 * @param professionId
	 * @return List<Map<String,Object>> userList
	 * @throws Exception
	 */
	public List<Map<String, Object>> getUserByProfessionId(Map<String, Object> paramMap) throws Exception {

		List<Map<String, Object>> returnList = new ArrayList<Map<String, Object>>();
		try {
			returnList = professionDao.getUserByProfessionId(paramMap);
		} catch (Exception e) {
			logger.error("查询某个专业下的教师信息异常getUserByProfessionId", e);
			throw e;
		}
		return returnList;
	}
	
	/**
	 * @description 查询某个专业下的班级
	 * @param professionId
	 * @return List<Map<String,Object>> userList
	 * @throws Exception
	 */
	public List<Map<String, Object>> getClassByProfessionId(Map<String, Object> paramMap) throws Exception {

		List<Map<String, Object>> returnList = new ArrayList<Map<String, Object>>();
		try {
			returnList = professionDao.getClassByProfessionId(paramMap);
		} catch (Exception e) {
			logger.error("查询某个专业下的班级异常getClassByProfessionId", e);
			throw e;
		}
		return returnList;
	}

	/**
	 * @description 获取专业下的地点信息
	 * @param professionId
	 * @return List<Map<String,Object>> userList
	 * @throws Exception
	 */
	public List<Map<String, Object>> getSiteByProfessionId(Map<String, Object> paramMap) throws Exception {

		List<Map<String, Object>> returnList = new ArrayList<Map<String, Object>>();
		try {
			returnList = professionDao.getSiteByProfessionId(paramMap);
		} catch (Exception e) {
			logger.error("获取专业下的地点信息getSiteByProfessionId", e);
			throw e;
		}
		return returnList;
	}
	
}